package com.tanhua;

import cn.hutool.core.io.FileUtil;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import com.tanhua.template.AipFaceTemplate;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.util.HashMap;

@RunWith(SpringRunner.class)
@SpringBootTest
public class AipFaceDemo {

    @Test
    public void test1(){
        //人脸客户端
        AipFace client = new AipFace("23491979", "ruTX3wgWnUxnXc4m4GEZHYFZ", "lshdY7gY23Tn2YdOtws9Gmv73iiFTURD");

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        File file = new File("C:\\Users\\muzimoo\\Desktop\\图片\\111.jpg");
        byte[] bytes = FileUtil.readBytes(file);
        String image =Base64Util.encode(bytes);


        String imageType = "BASE64";

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        System.out.println(res.toString(2));
    }

    @Autowired
    private AipFaceTemplate aipFaceTemplate;
    @Test
    public void test2(){

        String filename = "C:\\Users\\muzimoo\\Desktop\\图片\\111.jpg";
        File file = new File(filename);
        byte[] bytes = FileUtil.readBytes(file);
        System.out.println(aipFaceTemplate.detect(bytes));
    }
}
